import * as THREE from 'three';
import CimOther from "./CimOther";
import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader.js'
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader.js'
class CimEntity {
    constructor(config) {
        this.id = config.id
        this.entityName = config.name
        this.lod = config.lod
        this.position = config.position
        this.rotation = config.rotation
        this.model_3d = config.model_3d[0]
        this.defaultMaterial = '备用材质地址'
        this.children = []
        this.threeModel = ''
        this.live = false
        this.visible=false

        // 私有方法
        const addLine = (color, geometry) => {
            const edges = new THREE.EdgesGeometry(geometry);
            const edgesMaterial = new THREE.LineBasicMaterial({
                color: color,
                opacity: 0.5,
            });
            const line = new THREE.LineSegments(edges, edgesMaterial);
            return line;
        };

        // 将私有方法绑定到实例
        this._addLine = addLine;
    }

    create3DEntity() {
        let entity = null
        if (this.model_3d.model_url ) {
            //有三维模型
            const timestamp = new Date().getTime(); 
            entity = this.loadModel("/testModel/" + this.model_3d.model_url +`?timestamp=${timestamp}`, "/testModel/" + this.model_3d.material_url || this.defaultMaterial, this.model_3d.position || { x: 0, y: 0, z: 0 }, this.model_3d.rotation || { x: 0, y: 0, z: 0 })
        }
        //console.log(entity);
        this.threeModel = entity
        return entity
    }

    loadModel(obj_url, mtl_url, position, rotation) {
        
        var loader = new OBJLoader();
        var mloader = new MTLLoader();
        const model_3d = new THREE.Object3D();
        model_3d.name = this.entityName;
        let that = this
        mloader.load(mtl_url, (materials) => {
            materials.preload();
            loader.setMaterials(materials);
            loader.load(obj_url, (obj) => {
                let line = that._addLine("#000000", obj.children[0].geometry);
                obj.add(line);
                model_3d.add(obj);
            });
        });

        model_3d.position.set(position.x, position.y, position.z);
        model_3d.rotateX(rotation.x)
        model_3d.rotateY(rotation.y)
        model_3d.rotateZ(rotation.z)
        return model_3d
    }


    addModel(scene) {
        if (!this.live) {
            this.create3DEntity()
            this.threeModel.position.set(
                this.position.x,
                this.position.y,
                this.position.z
            );
            scene.add(this.threeModel)
            this.live=true
        }

    }

    hideModel() {
        this.threeModel.visible = false
        this.visible=false
    }
    showModel() {
        this.threeModel.visible = true
        this.visible=true
    }
    removeModel(scene) {
        if (this.live) {
            // this.threeModel.traverse(function (obj) {
            //     if (obj.type === 'Mesh' || obj.type === 'LineSegments') {
            //         console.log('是删除对象');
            //         obj.geometry.dispose();
            //         //obj.material.dispose();
            //     }
            //     obj=null
            // })
            scene.remove(this.threeModel)
            this.threeModel=null
            this.live=false
        }

    }

    //从数据层面删除实体
    deleteEntity(){
        //查找other节点
        let other=this.parent
        //获取顶层节点
        while(!other.scene_id){
            other=other.parent
        }


        while(this.lod != other.lod){
            other=other.children.find(child =>
                child instanceof CimOther
            )
        }

        //孩子转移给other
        other.children=other.children.concat(this.children)
        //从父节点删除掉这个实体孩子
        const index = this.parent.children.indexOf(this)
        if(index!=-1){
            this.parent.children.splice(index,1)
        }
        //移除其他内容
        this.parent=null
        this.children=null
    }

}

export default CimEntity